package com.xc.smart.controller;


import com.xc.smart.entity.User;
import com.xc.smart.service.RoleService;
import com.xc.smart.service.UserService;
import com.xc.smart.utlis.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
用户权限管理
 * @author mtl
 * 2021/6/15
 */

@Slf4j
@Validated
@RestController
@RequestMapping("/auth/")
public class AuthController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;


    /**
     * @description 添加用户
     * @param userDTO
     * @return
     */
    @PostMapping("user")
    public R add(@RequestBody User userDTO){
        userService.save(userDTO);
        return R.ok();
    }

    /**
     * @description 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("user/{id}")
    public R deleteUser(@PathVariable("id")Integer id){
        userService.removeById(id);
        return R.ok();
    }

    /**
     * @descripiton 修改用户
     * @param user
     * @return
     */
    @PutMapping("user")
    public R updateUser(@RequestBody User user){
        userService.updateById(user);
        return R.ok();
    }

    /**
     * @description 获取用户列表
     * @return
     */
    @GetMapping("user")
    public R findAllUser(){
        return R.ok().put("list",userService.list());
    }

    /**
     * @description 用户登录
     * @param
     * @return
     */
    @PostMapping("user/login")
    public R login(@RequestParam String username,
                   @RequestParam String password,
                   HttpServletRequest request){
        return userService.login(username,password,request);
    }


    /**
     * @description 用户注销
     * @param authorization
     * @return
     */
    @GetMapping("user/logout")
    public R logout(@RequestHeader("Authorization") String authorization){
       // redisTokenStore.removeAccessToken(AssertUtils.extracteToken(authorization));
        return R.ok();
    }

    /**
     * @description 用户刷新Token
     * @param refreshToken
     * @return
     */
    @GetMapping("user/refresh/{refreshToken}")
    public R refresh(@PathVariable(value = "refreshToken") String refreshToken){
        return null;
    }


    /**
     * @description 获取所有角色列表
     * @return
     */
    @GetMapping("role")
    public R findAllRole(){
        return R.ok().put("list",roleService.list());
    }


}